

function say() {
    alert("LC:515. 在每个树行中找最大值");
}
say();

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[]}
 */
var largestValues = function(root) {
    //层次遍历
    if(!root){
        return []
    }
    var levelNum = -Infinity
    var rootArr = [root, null]
    var resArr = []
    while(rootArr.length){
        var first = rootArr.shift()
        if(first) {
            levelNum = levelNum > first.val ? levelNum : first.val
            if(first.left) {
                rootArr.push(first.left)
            }
            if(first.right){
                rootArr.push(first.right)
            }
        }else {
            resArr.push(levelNum)
            levelNum = -Infinity
            if(rootArr.length) {
                rootArr.push(null)
            }
        }
    }
    return resArr
};
